package com.vincent.web;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

/**
 * Created by vincent on 2018/7/14.
 */
@RestController
@RequestMapping("/redis")
public class RedisController {
    private static final Logger logger = LoggerFactory.getLogger(RedisController.class);
    @Autowired
    private RedisConnectionFactory connectionFactory;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation(value = "添加缓存byConnection", notes = "添加redis缓存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "键", required = true, dataType = "String"),
            @ApiImplicitParam(name = "value", value = "值", required = true, dataType = "String")
    })
    @GetMapping("/add")
    public boolean add(@RequestParam String key, @RequestParam String value) {
        try {
            return connectionFactory.getClusterConnection().set(key.getBytes("UTF-8"), value.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            logger.error("写入失败", e);
            return false;
        }
    }
    @ApiOperation(value = "添加缓存byTemplate", notes = "添加redis缓存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "键", required = true, dataType = "String"),
            @ApiImplicitParam(name = "value", value = "值", required = true, dataType = "String"),
            @ApiImplicitParam(name = "ttl", value = "失效时间,单位为秒,-1为不失效", required = true, dataType = "long")
    })
    @GetMapping("/addByTemplate")
    public void addByTemplate(@RequestParam String key, @RequestParam String value, @RequestParam long ttl) {
        if (ttl <= 0)
            stringRedisTemplate.opsForValue().set(key, value);
        else
            stringRedisTemplate.opsForValue().set(key, value, ttl, TimeUnit.SECONDS);
    }
    @ApiOperation(value = "删除缓存byTemplate", notes = "删除redis缓存")
    @ApiImplicitParam(name = "key", value = "键", required = true, dataType = "String")
    @GetMapping("/del")
    public boolean del(@RequestParam String key){
        return stringRedisTemplate.delete(key);
    }

    @ApiOperation(value = "获取缓存失效时间", notes = "获取缓存失效时间")
    @ApiImplicitParam(name = "key", value = "键", required = true, dataType = "String")
    @GetMapping("/ttl")
    public long ttl(@RequestParam String key) {
        return stringRedisTemplate.getExpire(key);
    }
    @ApiOperation(value = "获取缓存", notes = "获取缓存")
    @ApiImplicitParam(name = "key", value = "键", required = true, dataType = "String")
    @GetMapping("/get")
    public String get(@RequestParam String key) {
        String s = stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isEmpty(s)){
            return "";
        } else {
            return s;
        }
    }
}
